package JET.geom;

/**
 *
 * @author 057Ry
 */
public final class Rect2d
{
    
    private Vec2d v1;  // lower left corner
    private Vec2d v2;  // upper right corner
    
    public Rect2d(double x1, double y1, double x2, double y2)
    {
        v1 = new Vec2d(x1,y1);
        v2 = new Vec2d(x2,y2);
    }

    public Rect2d(Vec2d inv1, Vec2d inv2)
    {
        Vec2d uv1 = new Vec2d(0.0, 1.0); // vertical unit vector
        Vec2d uv2 = new Vec2d(1.0, 0.0); // horizontal unit vector

        Vec2d vec = new Vec2d(inv2);
        vec.sub(inv1);

        double sp1 = vec.dot( uv1 );
        double sp2 = vec.dot( uv2 );

        // Rectangle is invalid
        if ( Math.abs(sp1) < 0.000001 || Math.abs(sp2) < 0.000001 )
        { /* TODO: zaimplementowac costam */ }

        if ( sp1 >= 0  &&  sp2 >= 0 )
        {
            v1 = new Vec2d(inv1);
            v2 = new Vec2d(inv2);
        }
        else if ( sp1 <= 0  &&  sp2 >=0 )
        {
            v1 = new Vec2d(inv1.getX(), inv2.getY());
            v2 = new Vec2d(inv2.getX(), inv1.getY());
        }
        else if ( sp1 >=0  &&  sp2 <=0 )
        {
            v1 = new Vec2d(inv2.getX(), inv1.getY());
            v2 = new Vec2d(inv1.getX(), inv2.getY());
        }
        else if ( sp1 <=0  &&  sp2 <=0 )
        {
            v1 = new Vec2d(inv2);
            v2 = new Vec2d(inv1);
        }
    }
    
    public boolean contains(Vec2d v2d)
    {
        return v2d.getX()>=v1.getX() && v2d.getY()>=v1.getY() &&
               v2d.getX()<=v2.getX() && v2d.getY()<=v2.getY() ;
    }

    public void setV1(Vec2d vec)
    {
        v1.become(vec);
    }
    public void setV2(Vec2d vec)
    {
        v2.become(vec);
    }

    public double getX1()
    {
        return v1.getX();
    }
    public double getY1()
    {
        return v1.getY();
    }
    public double getX2()
    {
        return v2.getX();
    }
    public double getY2()
    {
        return v2.getY();
    }
    
    public double getWidth()
    {
        return getX2() - getX1();
    }
    public double getHeight()
    {
        return getY2() - getY1();
    }
}
